package com.wcar.vipmanagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.FrontUserStore;
import com.wcar.common.entity.StoreUser;
import com.wcar.common.entity.User;
import com.wcar.common.entity.UserIntegral;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.vipmanagement.dao.FrontUserStoreDao;
import com.wcar.vipmanagement.dao.StoreUserDao;
import com.wcar.vipmanagement.dao.UserIntegralLogDao;
import com.wcar.vipmanagement.service.PointService;
import com.wcar.vipmanagement.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PointServiceImpl implements PointService {
    @Resource
    private UserIntegralLogDao userIntegralLogDao;
    @Autowired
    private FrontUserStoreDao frontUserStoreDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private StoreUserDao storeUserDao;
    public Result<Page<UserIntegral>> getPointList(UserVo userVo) {
        //获取token
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<StoreUser> storeUserWrapper = new QueryWrapper<>();
        if(user.getUserId()!=1){
            storeUserWrapper.eq("user_id",user.getUserId());
        }
        List<StoreUser> storeUsers = storeUserDao.selectList(storeUserWrapper);
        ArrayList<Integer> userIds = new ArrayList<>();
        for (StoreUser storeUser : storeUsers) {
            userIds.add(storeUser.getStoreId());
        }
        QueryWrapper<FrontUserStore> frontUserWrapper = new QueryWrapper<>();
        if(userIds.size()==0){
            return new Result<>(201,"查询数据成功",null);
        }
        frontUserWrapper.in("store_id",userIds);
        List<FrontUserStore> frontUserStores = frontUserStoreDao.selectList(frontUserWrapper);
        ArrayList<Integer> storeIds = new ArrayList<>();
        for (FrontUserStore frontUserStore : frontUserStores) {
            storeIds.add(frontUserStore.getUserId());
        }
        Page<UserIntegral> page = new Page<>(userVo.getCurrentPage(),userVo.getPageSize());
        QueryWrapper<UserIntegral> wrapper = new QueryWrapper<>();
        if(userVo.getUserName()!=null&&userVo.getUserName()!=""){
            wrapper.like("user_name",userVo.getUserName());
        }
        wrapper.in("tui.user_id",storeIds);
        Page<UserIntegral> list = userIntegralLogDao.getPage(page, wrapper);
        System.out.println(list.getRecords());
        return new Result<>(200,"获取明细信息成功",list);
    }
}
